﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace RockHopper.Common.Commanding
{
    public class DelegateCommand : ICommand
    {
        private readonly Action execute;
        private readonly Predicate<object> canExecute;
  
        public event EventHandler CanExecuteChanged;

        public DelegateCommand()
        {
        }

        public DelegateCommand(Action execute, Predicate<object> canExecute = null)
        {
            if (execute == null) throw new ArgumentNullException("execute");
            this.execute = execute;
            this.canExecute = canExecute;
        }

        //protected void WithActions(Action execute, Predicate<object> canExecute = null)
        //{
        //    if (execute == null) throw new ArgumentNullException("execute");
        //    this.execute = execute;
        //    this.canExecute = canExecute;
        //}

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        public void Execute(object parameter)
        {
            execute();
        }
    }
}